package GUI;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;

public class DialogInsertDB extends JDialog implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();
	private JTextField tfName;
	private JTextField tfPrice;
	private JTextField tfOwner;
	JButton okButton;
	JButton cancelButton;
	String[] allfileds = new String[3];
	
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			DialogInsertDB dialog = new DialogInsertDB();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public DialogInsertDB() {
		BuildGUI();
		this.setVisible(true);
	}
	public void BuildGUI(){
		setTitle("Insert to DB");
		setBounds(100, 100, 463, 145);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		tfName = new JTextField();
		tfName.setBounds(20, 23, 86, 20);
		contentPanel.add(tfName);
		tfName.setColumns(10);
		
		tfPrice = new JTextField();
		tfPrice.setBounds(126, 23, 86, 20);
		contentPanel.add(tfPrice);
		tfPrice.setColumns(10);
		
		tfOwner = new JTextField();
		tfOwner.setBounds(232, 23, 86, 20);
		contentPanel.add(tfOwner);
		tfOwner.setColumns(10);
		
		JLabel lblName = new JLabel("Name");
		lblName.setBounds(10, 11, 46, 14);
		contentPanel.add(lblName);
		
		JLabel labelOwner = new JLabel("Owner");
		labelOwner.setBounds(222, 11, 46, 14);
		contentPanel.add(labelOwner);
		
		JLabel lblPrice = new JLabel("Price");
		lblPrice.setBounds(116, 11, 46, 14);
		contentPanel.add(lblPrice);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				 okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
				okButton.addActionListener(this);
			}
			{
				 cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
				cancelButton.addActionListener(this);
			}
		}
	}
	@Override
	public void actionPerformed(ActionEvent event) {
		// TODO Auto-generated method stub
		if (event.getSource() == okButton){
			setAllFields();
			this.dispose();
			MainFrame.frame.setStringsToDB();
			
		}
		else if (event.getSource() == cancelButton){
			this.dispose();
		}
		
	}
	public void setAllFields(){
		 
		allfileds[0] = tfName.getText();
		allfileds[1] = tfPrice.getText();
		allfileds[2] = tfOwner.getText();
		
		
	}
	public String[] getAllFields(){
		String[] allfiledsLocal = new String[3];
		allfiledsLocal[0] = tfName.getText();
		allfiledsLocal[1] = tfPrice.getText();
		allfiledsLocal[2] = tfOwner.getText();
	//	new String[](3);
		return allfiledsLocal.clone();
		
	}
}
